package com.gxa.service.impl;

import com.gxa.dao.HouseDao;
import com.gxa.dao.impl.HouseDaoImpl;
import com.gxa.dto.HouseDto;
import com.gxa.entity.Build;
import com.gxa.entity.Community;
import com.gxa.entity.House;
import com.gxa.entity.Owner;
import com.gxa.entity.extend.QueryAllHouse;
import com.gxa.exception.ErrorType;
import com.gxa.exception.ServiceException;
import com.gxa.service.HouseService;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class HouseServiceImpl implements HouseService {
    private HouseDao houseDao = new HouseDaoImpl();
    @Override
    public List<House> queryHouse() {

        List<House> houseList = null;
        try {
            houseList = houseDao.queryHouse();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
        }
        return houseList;
    }

    @Override
    public List<House> queryAllHouse() {

        List<House> houses = null;
        try {
            houses = houseDao.queryAllHouse();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
        }
        return houses;
    }

    @Override
    public  List<House> queryAllHouseByPage(String page, String limit) {
        // 1、非空校验
        if (page == null || page.trim().isEmpty()){
            throw new ServiceException(ErrorType.EMPTY_PARAMETERS.getCode(),ErrorType.EMPTY_PARAMETERS.getMsg());
        }

        if (limit == null || limit.trim().isEmpty()) {
            throw new ServiceException(ErrorType.EMPTY_PARAMETERS.getCode(),ErrorType.EMPTY_PARAMETERS.getMsg());
        }

        // 2、参数转换
        int pageToInt = Integer.parseInt(page);
        int limitToInt = Integer.parseInt(limit);
        // 3、调用方法完成业务

        List<House> houseList = null;
        try {
            houseList = houseDao.selectAllHouseByPage(pageToInt, limitToInt);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
        }
        return houseList;
    }

    @Override
    public  List<House> queryHouseByHouseDto(String page, String limit, HouseDto houseDto) {
        // 1、非空校验
        if (page == null || page.trim().isEmpty()){
            throw new ServiceException(ErrorType.EMPTY_PARAMETERS.getCode(),ErrorType.EMPTY_PARAMETERS.getMsg());
        }

        if (limit == null || limit.trim().isEmpty()) {
            throw new ServiceException(ErrorType.EMPTY_PARAMETERS.getCode(),ErrorType.EMPTY_PARAMETERS.getMsg());
        }

        // 2、参数转换
        int pageToInt = Integer.parseInt(page);
        int limitToInt = Integer.parseInt(limit);
        // 3、调用方法完成业务
        List<House> houseList = null;
        try {
            houseList = houseDao.selectHouseByHouseDto(pageToInt, limitToInt, houseDto);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
        }
        return houseList;
    }

    @Override
    public Integer count() {

        Integer count = null;
        try {
            count = houseDao.count();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
        }
        return count;
    }

    @Override
    public Integer count(HouseDto houseDto) {

        Integer count = null;
        try {
            count = houseDao.count(houseDto);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
        }
        return count;
    }

    @Override
    public boolean addHouse(HouseDto houseDto) {
        // 1、提取参数
        String propertyId = houseDto.getPropertyId();
        Integer houseNum = houseDto.getHouseNum();
        Integer count = houseDto.getCount();
        Date liveTime = houseDto.getLiveTime();
        System.out.println(liveTime);
        // 小区
        String cid = houseDto.getCid();
        String cname = houseDto.getCname();
        // 栋数
        String bid = houseDto.getBid();
        String bname = houseDto.getBname();
        String unit = houseDto.getUnit();
        // 业主
        String oid = houseDto.getOid();
        String oname = houseDto.getOname();

        if (propertyId == null || propertyId.trim().isEmpty()) { // 房产编号
            throw new ServiceException(ErrorType.EMPTY_PARAMETERS.getCode(),ErrorType.EMPTY_PARAMETERS.getMsg());
        }

        //int houseNumToInt = 0;
        if (houseNum == null) {
            //int res = (houseNum == null ? 0 : houseNum);
            throw new ServiceException(ErrorType.EMPTY_PARAMETERS.getCode(),ErrorType.EMPTY_PARAMETERS.getMsg());
        }

        if (count == null) {
            throw new ServiceException(ErrorType.EMPTY_PARAMETERS.getCode(),ErrorType.EMPTY_PARAMETERS.getMsg());
        }

        if (liveTime == null) {
            throw new ServiceException(ErrorType.EMPTY_PARAMETERS.getCode(),ErrorType.EMPTY_PARAMETERS.getMsg());
        }
        // 小区
        if (cid == null || cid.trim().isEmpty()){
            throw new ServiceException(ErrorType.EMPTY_PARAMETERS.getCode(),ErrorType.EMPTY_PARAMETERS.getMsg());
        }

        if (cname == null || cname.trim().isEmpty()) {
            throw new ServiceException(ErrorType.EMPTY_PARAMETERS.getCode(),ErrorType.EMPTY_PARAMETERS.getMsg());
        }

        // 栋数
        if (bid == null || bid.trim().isEmpty()){
            throw new ServiceException(ErrorType.EMPTY_PARAMETERS.getCode(),ErrorType.EMPTY_PARAMETERS.getMsg());
        }

        if (bname == null || bname.trim().isEmpty()){
            throw new ServiceException(ErrorType.EMPTY_PARAMETERS.getCode(),ErrorType.EMPTY_PARAMETERS.getMsg());
        }

        if (unit == null || unit.trim().isEmpty()) {
            throw new ServiceException(ErrorType.EMPTY_PARAMETERS.getCode(),ErrorType.EMPTY_PARAMETERS.getMsg());
        }

        // 业主
        if (oid == null || oid.trim().isEmpty()) {
            throw new ServiceException(ErrorType.EMPTY_PARAMETERS.getCode(),ErrorType.EMPTY_PARAMETERS.getMsg());
        }

        if (oname == null || oid.trim().isEmpty()){
            throw new ServiceException(ErrorType.EMPTY_PARAMETERS.getCode(),ErrorType.EMPTY_PARAMETERS.getMsg());
        }

        // 2、转换参数
        // 小区
        Integer cidToInteger = Integer.valueOf(cid);
        // 栋数
        Integer bidToInteger = Integer.valueOf(bid);
        Integer unitToInteger = Integer.valueOf(unit);
        // 业主
        Integer oidToInteger = Integer.valueOf(oid);

        // 3、封装对象
        // 小区对象
        Community community = new Community(cidToInteger, cname);
        // 栋数对象
        Build build = new Build(bidToInteger, bname, unitToInteger);
        // 业主
        Owner owner = new Owner(oidToInteger, oname);
        House house = new House(propertyId, houseNum, count, liveTime, community, build, owner);

        // 4、调用Dao，完成新增
        int result = 0;
        try {
            result = houseDao.insertHouse(house);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
        }
        if (result != 0){
            return true;
        }
        return false;
    }

    @Override
    public boolean editHouse(House house) {
        // 1、提取参数
        String propertyId = house.getPropertyId();
        Integer houseNum = house.getHouseNum();
        Integer count = house.getCount();
        Date liveTime = house.getLiveTime();
        System.out.println(liveTime);
        // 小区
        Community community = house.getCommunity();

        // 栋数
        Build build = house.getBuild();

        // 业主
        Owner owner = house.getOwner();


        if (propertyId == null || propertyId.trim().isEmpty()) { // 房产编号
            throw new ServiceException(ErrorType.EMPTY_PARAMETERS.getCode(),ErrorType.EMPTY_PARAMETERS.getMsg());
        }

        //int houseNumToInt = 0;
        if (houseNum == null) {
            //int res = (houseNum == null ? 0 : houseNum);
            throw new ServiceException(ErrorType.EMPTY_PARAMETERS.getCode(),ErrorType.EMPTY_PARAMETERS.getMsg());
        }

        if (count == null) {
            throw new ServiceException(ErrorType.EMPTY_PARAMETERS.getCode(),ErrorType.EMPTY_PARAMETERS.getMsg());
        }

        if (liveTime == null) {
            throw new ServiceException(ErrorType.EMPTY_PARAMETERS.getCode(),ErrorType.EMPTY_PARAMETERS.getMsg());
        }
        // 小区
        if (community == null){
            throw new ServiceException(ErrorType.EMPTY_PARAMETERS.getCode(),ErrorType.EMPTY_PARAMETERS.getMsg());
        }

        // 栋数
        if (build == null){
            throw new ServiceException(ErrorType.EMPTY_PARAMETERS.getCode(),ErrorType.EMPTY_PARAMETERS.getMsg());
        }

        if (owner == null){
            throw new ServiceException(ErrorType.EMPTY_PARAMETERS.getCode(),ErrorType.EMPTY_PARAMETERS.getMsg());
        }
        // 小区参数,非空校验
        Integer cid = community.getCid();
        String cname = community.getCname();
        if (cid == null) {
            throw new ServiceException(ErrorType.EMPTY_PARAMETERS.getCode(),ErrorType.EMPTY_PARAMETERS.getMsg());
        }
        if (cname == null || cname.trim().isEmpty()) {
            throw new ServiceException(ErrorType.EMPTY_PARAMETERS.getCode(),ErrorType.EMPTY_PARAMETERS.getMsg());
        }

        // 栋数参数，非空校验
        Integer bid = build.getBid();
        String bname = build.getBname();
        Integer unit = build.getUnit();

        if (bid == null) {
            throw new ServiceException(ErrorType.EMPTY_PARAMETERS.getCode(),ErrorType.EMPTY_PARAMETERS.getMsg());
        }
        if (bname == null || bname.trim().isEmpty()) {
            throw new ServiceException(ErrorType.EMPTY_PARAMETERS.getCode(),ErrorType.EMPTY_PARAMETERS.getMsg());
        }
        if (unit == null) {
            throw new ServiceException(ErrorType.EMPTY_PARAMETERS.getCode(),ErrorType.EMPTY_PARAMETERS.getMsg());
        }

        // 获取业主参数，非空校验
        Integer oid = owner.getOid();
        String oname = owner.getOname();
        if (oid == null) {
            throw new ServiceException(ErrorType.EMPTY_PARAMETERS.getCode(),ErrorType.EMPTY_PARAMETERS.getMsg());
        }

        if (oname == null || oname.trim().isEmpty()){
            throw new ServiceException(ErrorType.EMPTY_PARAMETERS.getCode(),ErrorType.EMPTY_PARAMETERS.getMsg());
        }


        // 4、调用Dao，完成修改
        int result = 0;
        try {
            result = houseDao.updateHouse(house);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
        }
        if (result != 0){
            return true;
        }
        return false;
    }

    @Override
    public boolean delHouseById(String hid) {
        // 1、非空校验
        if (hid == null || hid.trim().isEmpty()) {
            throw new ServiceException(ErrorType.EMPTY_PARAMETERS);
        }
        // 2、参数转化
        Integer hidToInteger = Integer.valueOf(hid);
        // 3、执行删除操作
        int delResult = 0;
        try {
            delResult = houseDao.delHouseById(hidToInteger);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
        }
        return delResult != 0;
    }
}
